﻿using System;
using System.Collections.Generic;
using System.Reflection;

namespace BGShell
{
    public abstract class BGShellAddIn
    {
        protected internal virtual Type[] GetTabTypes()
        {
            return new Type[0];
        }

        protected internal virtual void OnApplicationStartup(BGShellApplication application)
        {
        }

        internal static BGShellAddIn[] LoadFrom(Assembly assembly)
        {
            return LoadFrom(assembly, false);
        }

        internal static BGShellAddIn[] LoadFrom(Assembly assembly, Boolean includeInternal)
        {
            var addins = new List<BGShellAddIn>();

            if (assembly != null)
            {
                Type[] types;

                if (includeInternal)
                {
                    types = assembly.GetTypes();
                }
                else
                {
                    types = assembly.GetExportedTypes();
                }

                foreach (var type in types)
                {
                    if ((!type.IsAbstract) &&
                        (type.GetConstructor(Type.EmptyTypes) != null) &&
                        (typeof(BGShellAddIn).IsAssignableFrom(type)))
                    {
                        addins.Add((BGShellAddIn)Activator.CreateInstance(type));
                    }
                }
            }

            return addins.ToArray();
        }
    }
}
